package com.sumainfor.pc.dev.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.dev.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.dev.service.BannerService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-04-01 18:47
 **/
@Controller
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    BannerService bannerService;


    @Autowired
    ZpcMktFeign zpcMktFeign;
    /**
    * <p>
    * 根据上下架状态查询首页Banner
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @GetMapping("/getBannerStatus")
    public JsonResult getBannerStatus(@RequestParam Map<String,Object> params, Pager pager){
        JsonResult result = new JsonResult();
        System.out.println("sddsds"+params);
        pager.setPagerNecessary(params,pager);
        PageUtils pageUtils = new PageUtils();
        if(!FieldCheckUtil.isMapFieldNotNull(params ,new String[]{"State"})){
            return  result.putFailed("服务器繁忙...");
        }
        Integer cout =  bannerService.getBannerStatusCount(params);
        System.out.println("cout"+cout);
        if(cout == 0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(),params,cout);
        }else {
            List<Map<String, Object>> getBannerStatus = bannerService.getBannerStatus(params);
            return pageUtils.getJsonResult(getBannerStatus,params,cout);
        }
    }


    /**
    * <p>
    * 查询banner关联的房源
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @ResponseBody
    @PostMapping("/selectBanHouInfoId")
    public JsonResult selectBanHouInfoId(@RequestBody Map<String,Object> params){
        List<String> HouInfoIdList = new ArrayList<>();
        if(params.get("HouInfoId") !=null){
            HouInfoIdList.add(MapUtils.getString(params,"HouInfoId"));
        }
        JsonResult zpcResult = new JsonResult();
        if(HouInfoIdList.size()>0){
            params.put("HouInfoIdList",HouInfoIdList);
            zpcResult =zpcMktFeign.selectHousingHouInfoId(params);
        }
        return zpcResult;
    }

    /**
    * <p>
    * 更新首页Banner上下架状态和编辑banner
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
   @ResponseBody
   @PostMapping("/updateBannerStatus")
   public JsonResult updateBannerStatus(@RequestBody Map<String,Object> params){
       JsonResult result = new JsonResult();
      if(!FieldCheckUtil.isMapFieldNotNull(params ,new String[]{"BanId"})){
           return  result.putFailed("服务器繁忙...");
        }
       return bannerService.updateBannerStatus(params);
   }

   /**
   * <p>
   * 删除banner
   * </p>
   *
   * @author: zhoucong
   * @since:
   */
   @ResponseBody
   @PostMapping("/deleteBanner")
   public JsonResult deleteBanner(@RequestBody Map<String,Object> params){
       JsonResult result = new JsonResult();
       if(!FieldCheckUtil.isMapFieldNotNull(params ,new String[]{"BanId"})){
           return  result.putFailed("服务器繁忙...");
       }
       return bannerService.deleteBanner(params);
   }


   /**
   * <p>
   * 创建Banner
   * </p>
   *
   * @author: zhoucong
   * @since:
   */
   @ResponseBody
    @PostMapping("/insertBanner")
    public JsonResult insertBanner(@RequestBody Map<String,Object> params){
       return bannerService.insertBanner(params);
   }


}
